import { UserService } from './../../../../@core/services/user/user.service';
import { FileEnvironment } from 'src/environments/FileEnvironment';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { AuthService } from 'src/app/@core/services/auth.service';
import { LANGUAGES } from 'src/config/language-config';
import { I18nService } from 'ng-devui/i18n';
import { DialogService, DrawerService } from 'ng-devui';
import { SideSettingsComponent } from '../../side-settings/side-settings.component';
import { PersonalizeComponent } from '../../personalize/personalize.component';
import { User } from 'src/app/@core/data/user';
import { EditPasswordComponent } from 'src/app/pages/basic-data/employs/employs/edit-password/edit-password.component';
import { environment } from 'src/environments/environment';
@Component({
  selector: 'da-header-operation',
  templateUrl: './header-operation.component.html',
  styleUrls: ['./header-operation.component.scss'],
})
export class HeaderOperationComponent implements OnInit {
  user!: User;
  languages = LANGUAGES;
  language!: string;
  haveLoggedIn = false;
  noticeCount!: number;
  settingDrawer: any;
  headUrl: string = '';
  name: any;
  constructor(
    private drawerService: DrawerService,
    private route: Router,
    private authService: AuthService,
    private translate: TranslateService,
    private i18n: I18nService,
    private dialogService: DialogService,
    private UserService: UserService
  ) {}
  ngOnInit(): void {
    let userinfo = localStorage.getItem('userinfo');
    if (!userinfo) {
      userinfo = sessionStorage.getItem('userinfo');
    }
    this.user = userinfo ? JSON.parse(userinfo) : null;
    this.haveLoggedIn = true;
    this.headUrl = environment.modifiedApiUrl + this.user.headUrl;
    this.name = this.user.username;
  }
  // onLanguageClick(language: string) {
  //   this.language = language;
  //   localStorage.setItem('lang', this.language);
  //   this.i18n.toggleLang(this.language);
  //   this.translate.use(this.language);
  // }
  handleUserOps(operation: string) {
    switch (operation) {
      case 'logout': {
        this.haveLoggedIn = false;
        localStorage.removeItem('userinfo');
        sessionStorage.removeItem('userinfo');
        this.route.navigate(['/', 'login']);
        break;
      }
      default:
        break;
    }
  }
  handleNoticeCount(event: number) {
    this.noticeCount = event;
  }
  openSettingDrawer() {
    if (this.settingDrawer) {
      this.settingDrawer.drawerInstance.show();
    } else {
      this.settingDrawer = this.drawerService.open({
        drawerContentComponent: SideSettingsComponent,
        width: '350px',
        destroyOnHide: false,
        data: {
          close: () => {
            this.settingDrawer.drawerInstance.hide();
          },
        },
      });
    }
  }
  personalizeConfig() {
    this.dialogService.open({
      id: 'theme',
      width: '800px',
      maxHeight: '800px',
      title: '',
      content: PersonalizeComponent,
      backdropCloseable: true,
      draggable: false,
      onClose: () => {},
      buttons: [],
    });
  }
  PersonalCenter() {
    this.route.navigate([
      '/pages/basicdata/userCenter',
      this.user.employeeCode,
    ]);
  }
  config = {
    maxHeight: '400px',
    width: '400px',
    backdropCloseable: true,
  }; //弹窗配置
  EditPassword(dialogtype?: string) {
    let data = {
      employId: this.user.employeeCode,
      display: false,
    };
    const results = this.dialogService.open({
      id: 'department',
      ...this.config,
      title: '更新密码',
      data: data,
      content: EditPasswordComponent,
      dialogtype: dialogtype,
      onClose: () => {},
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if (
              results.modalContentInstance.PutPassword.oldPassword &&
              results.modalContentInstance.PutPassword.newPassword
            ) {
              this.UserService.ChangePassword(
                results.modalContentInstance.PutPassword,
                this.user.employeeCode
              ).subscribe(
                (data: any) => {
                  this.showToast('success', '成功', '密码修改成功');
                  results.modalInstance.hide();
                },
                (error: any) => {
                  if (error.status == 404) {
                    this.showToast('error', '失败', '服务器连接失败');
                  } else {
                    this.showToast('error', '失败', error.error);
                  }
                  results.modalInstance.hide();
                }
              );
            } else {
              this.showToast('error', '失败', '密码不能为空');
              data.display = true;
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  } //修改密码
  msgs: Array<Object> = [];
  showToast(type: any, summary: any, content: any) {
    switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  } //提示框
}
